//
//  ForeignCommentViewController.m
//  GoodBuy
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "ForeignCommentViewController.h"
#import "ForCommentTableViewCell.h"
#import "NetworkHandler.h"
#import "ForCommentObject.h"
#import "MJRefresh.h"

@interface ForeignCommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *ForCommentArray;



@end

@implementation ForeignCommentViewController

- (void)dealloc
{
    [_tableView release];
    [_ForCommentArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"评论页面";
    
    [self crerteTableView];
    [self getDataFromRows];
    [self addHeader];
    [self addFooter];
}

// ---------------判断无评论时弹出框
- (void)NoneComment
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无评论,抢沙发哦" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [self.view addSubview:alert];
    [alert show];
    [alert release];
}

-(void)getDataFromRows
{
    NSString *idUrl = [NSString stringWithFormat:@"http://api.smzdm.com/v1/comments?limit=20&article_id=%@&type=haitao",self.lastModel.article_id];
    NSLog(@"%@",idUrl);
    //获取数据
    [NetworkHandler requestWitURL:idUrl completion:^(id result) {
        self.ForCommentArray = [NSMutableArray array];
        NSDictionary *dic = [result objectForKey:@"data"];
        //        NSLog(@"%@",dic);
        NSArray *array = [dic objectForKey:@"rows"];
        //        NSLog(@"%@",array);
        for (NSDictionary *dic2 in array) {
            ForCommentObject *mode = [[ForCommentObject alloc]initWithDic:dic2];
            [_ForCommentArray addObject:mode];
            //            NSLog(@"%@",_ForCommentArray);
        }
        if (_ForCommentArray.count == 0){
            [self NoneComment];
            [self.HUD removeFromSuperview];
        }else{
            [_tableView reloadData];
            [self.tableView headerEndRefreshing];
            [self.HUD removeFromSuperview];
        }

        
        [self.tableView footerEndRefreshing];
        [self.tableView headerEndRefreshing];
        [_tableView reloadData];
        self.listNumber = 20;
        [self.HUD removeFromSuperview];
        
    }];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.labelText = @"亲 请稍等";
    _HUD.tintColor = [UIColor cyanColor];
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //刷新时间最大上限
        [self showtime];
        
    } completionBlock:^{
     
    }];
    
    
    
}

- (void)showtime
{
    sleep(10);
}
#warning - mark - 下拉刷新
-(void)addHeader
{
    __block ForeignCommentViewController *qwe = self;
    //添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        qwe.isUploading = NO;
        [qwe getDataFromRows];
    }];
    
}

#warning mark - 上拉加载
- (void)addFooter
{
    __block ForeignCommentViewController *qwe = self;
    //添加上拉刷新尾部控件
    [self.tableView addFooterWithCallback:^{
        qwe.isUploading = YES;
        //标记为上拉操作
        [self getDataComment];
    }];
}

- (void)getDataComment
{
    
//    ForCommentObject *Model = [_ForCommentArray lastObject];
    NSString *idUrl = [NSString stringWithFormat:@"http://api.smzdm.com/v1/comments?offset=%ld&limit=20&article_id=%@&type=haitao",_listNumber,self.lastModel.article_id];
    NSLog(@"99999%@",idUrl);
    [NetworkHandler requestWitURL:idUrl completion:^(id result) {
        self.listNumber = self.listNumber + 20;
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"rows"];
        for (NSDictionary *dic2 in array) {
            ForCommentObject *model = [[ForCommentObject alloc] initWithDic:dic2];
            [_ForCommentArray addObject:model];
            [model release];
//            NSLog(@"%@",_ForCommentArray);
        }
        [self.tableView footerEndRefreshing];
        [self.tableView headerEndRefreshing];
        [_tableView reloadData];
    }];
   
}



- (void)crerteTableView
{
    self.tableView= [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.rowHeight = 160;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[ForCommentTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat F = [ForCommentTableViewCell heightWithText:[_ForCommentArray[indexPath.row] comment_content]];
//    NSLog(@"%lf", F);
    return F + 130;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ForCommentArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    ForCommentObject *YB = [[[ForCommentObject alloc] init] autorelease];
    YB = [_ForCommentArray objectAtIndex:indexPath.row];

    [cell setStu:YB];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
